<template>
  <el-dialog
    :title="'同步计费点'"
    :visible="visible"
    width="550px"
    :lock-scroll="false"
    @update:visible="updateVisible"
    :close-on-click-modal="false"
  >
    <el-form ref="form" :model="form" :rules="rules" label-width="100px">
      <el-form-item label="渠道:" prop="channel_id">
        <PageSelect
          :placeholder="'请选择所属渠道'"
          v-model="form.channel_id"
          :whereName="'channel_id'"
          :url="'/channel/index'"
          :optionLable="'name'"
          :optionvalue="'id'"
          :multiple="false"
        />
      </el-form-item>
    </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button type="primary" @click="save" :loading="loading"
        >保存
      </el-button>
    </div>
  </el-dialog>
</template>
<script>
import PageSelect from '@/components/pageSelect/index.vue'
export default {
  components: { PageSelect },
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      // 表单数据
      form: Object.assign({}),
      // 表单验证规则
      rules: {
        channel_id: [{ required: true, message: '请选择渠道', trigger: 'blur' }]
      },
      loading: false,
      packageId: ''
    }
  },
  watch: {
    visible() {
      if (this.visible) {
        this.form = Object.assign({})
        this.isUpdate = true
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value)
    },
    //保存
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.loading = true
          let params = {
            id: this.data.id,
            channel_id: this.form.channel_id
          }
          this.$http
            .post('/Package/setProduct', params)
            .then((res) => {
              if (res.data.code === 0) {
                this.$message.success(res.data.msg)
                this.$emit('done')
                this.updateVisible(false)
                this.loading = false
              } else {
                this.$message.error(res.data.msg)
              }
            })
            .catch((e) => {
              this.loading = false
              this.$message.error(e.message)
            })
        }
      })
    }
  }
}
</script>
<style scoped>
</style>